//
//  People+CoreDataProperties.m
//  WidgetTest
//
//  Created by 李明明 on 15/10/28.
//  Copyright © 2015年 李明明. All rights reserved.
//
//  Choose "Create NSManagedObject Subclass…" from the Core Data editor menu
//  to delete and recreate this implementation file for your updated model.
//

#import "People+CoreDataProperties.h"
#import "DataBaseOB.h"



@implementation People (CoreDataProperties)

@dynamic name;
@dynamic sex;
@dynamic age;
@dynamic feed;

+ (instancetype)sharePeople
{
    static People *people;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSManagedObjectContext *context = [DataBaseOB shareInstance].managedObjectContext;
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"People" inManagedObjectContext:context];
        people = [[People alloc] initWithEntity:entity insertIntoManagedObjectContext:nil];
    });
    return people;
}


- (void)insert:(NSDictionary *)info
{
    NSManagedObjectContext *context = [DataBaseOB shareInstance].managedObjectContext;
    NSManagedObject *people = [NSEntityDescription insertNewObjectForEntityForName:@"People" inManagedObjectContext:context];
    [people setValue:info[@"name"] forKey:@"name"];
    [people setValue:info[@"sex"] forKey:@"sex"];
    [people setValue:info[@"age"] forKey:@"age"];
    NSError *error;
    if ([context save:&error]) {
        NSLog(@"insert success");
    }else {
        NSLog(@"insert error:%@", error);
    }
}
- (void)deleteWith:(NSString *)rmObject
{
    NSManagedObjectContext *context = [DataBaseOB shareInstance].managedObjectContext;
    NSEntityDescription *people = [NSEntityDescription entityForName:@"People" inManagedObjectContext:context];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:people];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name=%@", rmObject];
    [request setPredicate:predicate];
    NSManagedObject *obj = [[context executeFetchRequest:request error:nil] lastObject];
    if (obj) {
        NSError *error;
        [context deleteObject:obj];
        if ([context save:&error]) {
            NSLog(@"delete success");
        }else {
            NSLog(@"delete error:%@", error);
        }
    }
}
- (void)selectField:(NSString *)field currentValue:(NSString *)currentValue aNewValue:(NSString *)anew
{
    NSManagedObjectContext *context = [DataBaseOB shareInstance].managedObjectContext;
    NSEntityDescription *people = [NSEntityDescription entityForName:@"People" inManagedObjectContext:context];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:people];
    if (field.length > 0) {
        NSString *sql = [NSString stringWithFormat:@"%@=\"%@\"", field,currentValue];
        NSPredicate *predicate = [NSPredicate predicateWithFormat:sql];
        [request setPredicate:predicate];
    }
    NSArray *peoArr = [context executeFetchRequest:request error:nil];
    for (NSManagedObject *entity in peoArr) {
        if (anew.length > 0) {
            [entity setValue:anew forKey:field];
        }
        NSLog(@"%@,%@,%@", [entity valueForKey:@"name"], [entity valueForKey:@"sex"], [entity valueForKey:@"age"]);
    }
    [context save:nil];

}
- (void)selectAll:(NSString *)str
{
    
}

@end
